import * as api from '../../utils/api.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo:wx.getStorageSync('userinfo'),
    isShowWithdraw:false
  },
  nextaddOrder:function(){
    wx.navigateTo({
      url: '/pages/business/addorder',
    })
  },
  nextseeOrder:function(){
    wx.navigateTo({
      url: '/pages/business/installOrderList',
    })
  },
  nextseeReserve:function(){
    wx.navigateTo({
      url: '/pages/business/productOrderList',
    })
  },
  cash:function(){
    wx.navigateTo({
      url: '/pages/business/cash',
    })
  },

  getUserInfo(){
    const that = this
    api.getUserInfo()
          .then(res => {
            that.setData({
              userinfo:res.data
            })
          }).catch(err => console.log(err))
  },

  getScanCode(){
    wx.scanCode({
      onlyFromCamera: true,
      success (res) {
        console.log(res)
        if(res.errMsg == "scanCode:ok"){
          const param = res.path.split('?')
          const param_str = param[1].substr(6)
          const param_arr = param_str.split('&')
          const order_sn = param_arr[0].split('=')[1]
          const code = param_arr[1].split('=')[1]
          api.businessProductOrderDetail(order_sn)
            .then(res => {
              if(res.code == 1) {
                wx.navigateTo({
                  url:'/' + param[0] + '?order_sn=' + order_sn + '&code=' + code
                })
              }
            }).catch(err => {
              wx.showToast({
                title: '非本门店订单',
                icon: 'none'
              })
            })
        }
      }
    })
  },

  isShowWithdraw(){
    const that = this
    api.isShowWithdraw()
          .then(res => {
            console.log(res)
            that.setData({
              isShowWithdraw:res.data.show
            })
          }).catch(err => console.log(err))
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getUserInfo()
    this.isShowWithdraw()
    wx.hideHomeButton({
      complete:()=>{
      }
    })
  },

  password (){
    wx.navigateTo({
      url: '/pages/myinfo/personaldata',
    })
  },

  logout(){
    const that = this
    api.userLogout().then(res => {
      if(res.code == 1) {
        wx.setStorageSync('token', '')
        wx.setStorageSync('userinfo', '')
        wx.setStorageSync('loginType', '')
        wx.setStorageSync('logs', '')
        wx.showToast({
          title: '退出成功',
          icon: 'none'
        })
        setTimeout(function(){
          wx.reLaunch({
            url: '/pages/home/home',
          })
        },1500)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})